package com.itheima.servlet02;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/studentServlet02")
public class StudentServlet02 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求体中表单的数据
        String username = request.getParameter("username");
        String age = request.getParameter("age");
        String scare = request.getParameter("scare");
//        //获取ServletContext对象
//        ServletContext context = super.getServletContext();
//        //获取全局配置users键对应的值
//        String parameter = context.getInitParameter("users");
        try {
            //获取缓冲字符输出流,目标为web目录(绝对路径),设置为追加输入
            BufferedWriter bw = new BufferedWriter(new FileWriter("D:/Work/IDEA2018/code/myWeb/day02-homework/web/1.txt", true));
            bw.write(username+"---"+age+"---"+scare);
            bw.close();
            //写入成功的反馈
            PrintWriter writer = response.getWriter();
            writer.print("write succes");
        } catch (IOException e) {
            //写入失败的反馈
            PrintWriter writer = response.getWriter();
            writer.print("write defeated");
            e.printStackTrace();
        }
    }
}
